<!-- src/views/Home.vue (增强版) -->
<template>
  <div class="home">
    <div class="welcome-container">
      <el-page-header @back="goBack" content="系统首页" v-if="showBackButton">
      </el-page-header>

      <div class="welcome-header">
        <h1>欢迎使用用户管理系统</h1>
        <p v-if="user">您好，{{ user.username }}！</p>
        <p>这是一个基于 Vue.js 和 Spring Boot 构建的用户管理应用</p>
      </div>

      <el-row :gutter="20" class="stats-container">
        <el-col :span="8">
          <el-card class="stat-card">
            <div class="stat-content">
              <i class="el-icon-user stat-icon"></i>
              <div class="stat-info">
                <div class="stat-number">{{ userCount }}</div>
                <div class="stat-label">总用户数</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="8">
          <el-card class="stat-card">
            <div class="stat-content">
              <i class="el-icon-monitor stat-icon"></i>
              <div class="stat-info">
                <div class="stat-number">Vue + Spring Boot</div>
                <div class="stat-label">技术栈</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="8">
          <el-card class="stat-card">
            <div class="stat-content">
              <i class="el-icon-check stat-icon"></i>
              <div class="stat-info">
                <div class="stat-number">稳定</div>
                <div class="stat-label">系统状态</div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>

      <div class="actions-container">
        <el-button type="primary" @click="goToUsers" size="large">
          <i class="el-icon-user"></i>
          查看用户列表
        </el-button>
        <el-button @click="logout" size="large">
          <i class="el-icon-switch-button"></i>
          退出登录
        </el-button>
      </div>

      <div class="info-section">
        <el-collapse v-model="activeNames">
          <el-collapse-item title="系统功能介绍" name="1">
            <div>本系统提供完整的用户管理功能，包括：</div>
            <div>1. 用户登录认证</div>
            <div>2. 用户列表查看</div>
            <div>3. 用户信息管理</div>
          </el-collapse-item>
          <el-collapse-item title="技术支持" name="2">
            <div>前端技术：Vue.js 2 + Element UI</div>
            <div>后端技术：Spring Boot + MyBatis</div>
            <div>数据库：MySQL</div>
          </el-collapse-item>
        </el-collapse>
      </div>
    </div>
  </div>
</template>

<script>
import request from '@/utils/request'

export default {
  name: 'Home',
  data() {
    return {
      user: null,
      userCount: 0,
      activeNames: ['1'],
      showBackButton: false
    }
  },
  created() {
    // 获取当前登录用户信息
    const userStr = localStorage.getItem('user')
    if (userStr) {
      try {
        this.user = JSON.parse(userStr)
      } catch (e) {
        console.error('解析用户信息失败:', e)
      }
    }

    // 获取用户总数
    this.fetchUserCount()
  },
  methods: {
    fetchUserCount() {
      request.get('/api/users', { withCredentials: true })
          .then(res => {
            if (res.success && Array.isArray(res.data)) {
              this.userCount = res.data.length
            }
          })
          .catch(err => {
            console.error('获取用户统计失败:', err)
          })
    },
    goToUsers() {
      this.$router.push('/users')
    },
    logout() {
      localStorage.removeItem('user')
      localStorage.removeItem('token')
      this.$router.push('/login')
    },
    goBack() {
      this.$router.go(-1)
    }
  }
}
</script>

<style scoped>
.home {
  padding: 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  min-height: calc(100vh - 60px);
}

.welcome-container {
  max-width: 1000px;
  margin: 0 auto;
  text-align: center;
}

.welcome-header h1 {
  color: white;
  font-size: 2.5em;
  margin-bottom: 10px;
  text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}

.welcome-header p {
  color: rgba(255,255,255,0.9);
  font-size: 18px;
  margin-bottom: 30px;
}

.stats-container {
  margin: 40px 0;
}

.stat-card {
  border: none;
  border-radius: 10px;
  box-shadow: 0 4px 8px rgba(0,0,0,0.1);
  transition: transform 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-5px);
}

.stat-content {
  display: flex;
  align-items: center;
  justify-content: center;
}

.stat-icon {
  font-size: 40px;
  margin-right: 15px;
  color: #409EFF;
}

.stat-info {
  text-align: left;
}

.stat-number {
  font-size: 24px;
  font-weight: bold;
  color: #303133;
}

.stat-label {
  font-size: 14px;
  color: #909399;
}

.actions-container {
  margin: 40px 0;
}

.actions-container .el-button {
  margin: 0 15px;
  padding: 12px 24px;
}

.info-section {
  margin-top: 40px;
  text-align: left;
}

.info-section >>> .el-collapse-item__header {
  font-size: 16px;
  font-weight: bold;
}

@media (max-width: 768px) {
  .welcome-header h1 {
    font-size: 2em;
  }

  .stats-container {
    flex-direction: column;
  }

  .actions-container .el-button {
    display: block;
    margin: 10px auto;
    width: 80%;
  }
}
</style>
